home *** CD-ROM | disk | FTP | other *** search
/ Nebula 2 / Nebula Two.iso / SourceCode / PopUpFormatter / RandomDataSource.m < prev    next >
Text File  |  1995-06-12  |  3KB  |  132 lines

  1. /* RandomDataSource.m:
  2.  * You may freely copy, distribute, and reuse the code in this example.
  3.  * NeXT disclaims any warranty of any kind, expressed or  implied, as to its
  4.  * fitness for any particular use.
  5.  *
  6.  * 
  7.  *
  8.  */
  9.  
  10. #import <appkit/appkit.h>
  11. #import <objc/List.h>
  12.  
  13. #import "RandomDataSource.h"
  14. #import "StringList.h"
  15.  
  16. @implementation RandomDataSource
  17.  
  18. - init
  19. {
  20.     [super init];
  21.     rowList = [[List alloc] init];
  22.     return self;
  23. }
  24.  
  25.  
  26.  
  27. - setRows:(unsigned int) rowCount
  28. {
  29.     rows = rowCount;
  30.     return self;
  31. }
  32.  
  33. - setColumns:(unsigned int) columnCount
  34. {
  35.     columns = columnCount;
  36.     return self;
  37. }
  38.  
  39. - (unsigned int) rowCount
  40. {
  41.     return rows;
  42. }
  43.  
  44. - (unsigned int) columnCount
  45. {
  46.     return columns;
  47. }
  48.  
  49.  
  50. - (const char *) getRandomString
  51. {
  52.     static const char       *text = NULL;
  53.     static unsigned int    totalSize;
  54.     char            buffer[100], *buf = buffer;
  55.     const char        *probe;
  56.  
  57.     if (text == NULL) {
  58.         int dummy; /* Not used */
  59.         NXStream *stream = NXMapFile ("/usr/dict/words", NX_READONLY);
  60.         if (!stream) return ""; /* Shouldn't happen... */
  61.         NXGetMemoryBuffer(stream, &text, &totalSize, &dummy);
  62.         NXCloseMemory(stream, NX_SAVEBUFFER); /* Free the stream but not the contents */
  63.         srandom(time(0)); /* Randomize the random number generator */
  64.     }    
  65.  
  66.     probe = (char *) (text + random() % (totalSize - 10));
  67.     while (*probe++ != '\n');
  68.     while (*probe != '\n' && buf - 99 < buffer) *buf++ = *probe++;
  69.     *buf = 0;
  70.  
  71.     return NXCopyStringBuffer(buffer);
  72. }
  73.  
  74.  
  75. - empty
  76. {
  77.     unsigned int maxRow = [rowList count];
  78.     unsigned int maxColumn = maxRow > 0 ? [[rowList objectAt:0] count] : 0;
  79.     unsigned int i, j;
  80.  
  81.     for (i=0;  i < maxRow;  i++)
  82.         for (j=0;  j < maxColumn;  j++)
  83.             free([[rowList objectAt:i] objectAt:j]);
  84.     
  85.     [rowList freeObjects];
  86.     return self;
  87. }
  88.  
  89.  
  90. - loadData
  91. {
  92.     unsigned int i, j;
  93.     
  94.     [self empty];
  95.     for (i=0;  i < rows;  i++) {
  96.         [rowList insertObject:[[StringList alloc] initCount:columns] at:i];
  97.         for (j=0;  j < columns;  j++) {
  98.             if (j != 1)
  99.                 [[rowList objectAt:i] addString:[self getRandomString] at:j];
  100.             else
  101.                 [[rowList objectAt:i] addString:"inches" at:j];
  102.  
  103.         }
  104.     }
  105.     
  106.     return self;
  107. }
  108.  
  109. /* This method is called to display the strings in the tableview */
  110. - getValueFor:index at:(unsigned int) aPosition into:aValue
  111. {
  112.     [aValue setStringValue:(char *) [[rowList objectAt:aPosition] stringAt:(int) index]];
  113.     return self;
  114. }
  115.  
  116. /* This method is called whenever the string is edited in the tableview */
  117. - setValueFor:index at:(unsigned int) aPosition from:aValue
  118. {
  119.     char    *string;
  120.     
  121.     string = (char *)[[rowList objectAt:aPosition] removeStringAt:(int) index];
  122.     free(string);
  123.     string = NXCopyStringBuffer([aValue stringValue]);
  124.     [[rowList objectAt:aPosition]
  125.         addString:(const char *) string at: (int) index];
  126.          
  127.     return self;
  128. }
  129.  
  130.  
  131. @end
  132.